﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.IO;

namespace com.sharepointutils.application2007
{
    public class SP07ListItemCollection : SP07Base
    {
        public SP07List List { get; set; }
        public List<SP07ListItem> Items = new List<SP07ListItem>();
        
        public SP07ListItemCollection (SP07List list)
        {
            List = list;
            Context = list.Context;
        }

        public override void Load()
        {
            Request.Username = Context.Username;
            Request.Domain = Context.Domain;
            Request.Password = Context.Password;

            if (Request.Run())
            {
               ParseXMLReponse(Request.Reponse);
            }
        }


        protected override void ParseXMLReponse(String xmlString)
        {
            using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
            {
                XmlWriterSettings ws = new XmlWriterSettings();
                ws.Indent = true;
                    // Parse the file and display each of the nodes.
                    while (reader.Read())
                    {
                        switch (reader.NodeType)
                        {
                            case XmlNodeType.Element:
                                if (reader.Name == "z:row" && reader.HasAttributes)
                                {
                                    SP07ListItem item = new SP07ListItem();
                                    while (reader.MoveToNextAttribute())
                                    {
                                        item[reader.Name.Substring(4)] = reader.Value;
                                    }
                                    Items.Add(item);
                                    reader.MoveToElement();
                                }
                                //writer.WriteStartElement(reader.Name);
                                break;
                            case XmlNodeType.Text:
                                //writer.WriteString(reader.Value);
                                break;
                            case XmlNodeType.XmlDeclaration:
                            case XmlNodeType.ProcessingInstruction:
                                //writer.WriteProcessingInstruction(reader.Name, reader.Value);
                                break;
                            case XmlNodeType.Comment:
                                //writer.WriteComment(reader.Value);
                                break;
                            case XmlNodeType.EndElement:
                                //writer.WriteFullEndElement();
                                break;
                        }
                    }

                
            }

        }


    }
}
